package com.tebiecloud.yunst.server.controller.feign;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.DateUtils;
import com.tebiecloud.common.utils.ResultBodyUtils;
import com.tebiecloud.yunst.client.constants.ResultEnum;
import com.tebiecloud.yunst.client.model.entity.YunAgentCollect;
import com.tebiecloud.yunst.client.model.entity.YunRefund;
import com.tebiecloud.yunst.client.model.entity.YunStUser;
import com.tebiecloud.yunst.client.model.vo.RefundVO;
import com.tebiecloud.yunst.client.service.IYunRefundServiceClient;
import com.tebiecloud.yunst.server.service.YunAgentCollectService;
import com.tebiecloud.yunst.server.service.YunRefundService;
import com.tebiecloud.yunst.server.service.YunStUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@Slf4j
@RestController
@RequestMapping("/feign/refund")
public class RefundController implements IYunRefundServiceClient {


    @Autowired
    YunRefundService yunRefundService;

    @Autowired
    YunAgentCollectService yunAgentCollectService;

    @Autowired
    private YunStUserService yunStUserService;

    @PostMapping("create")
    @Override
    public ResultBody<RefundVO> create(@RequestParam(value = "userId") Long userId, @RequestParam(value = "orderId")Long orderId, @RequestParam(value = "amount")int amount) {
        RefundVO refundVO = new RefundVO();
        //查询是否存在账号
        QueryWrapper<YunStUser> yunStUserQueryWrapper = new QueryWrapper<>();
        yunStUserQueryWrapper.eq("user_id",userId);
        YunStUser yunStUser = yunStUserService.getOne(yunStUserQueryWrapper);
        if(yunStUser==null){
            log.info(">>minjie "+ResultEnum.YUN_ST_USER_NOT_FIND.getMessage());
            return ResultBodyUtils.build(ResultEnum.YUN_ST_USER_NOT_FIND.getCode(),ResultEnum.YUN_ST_USER_NOT_FIND.getMessage());
        }
        //查询有无订单
        QueryWrapper<YunAgentCollect> yunAgentCollectQueryWrapper = new QueryWrapper<>();
        yunAgentCollectQueryWrapper.eq("order_id",orderId);
        yunAgentCollectQueryWrapper.eq("pay_status",1);
        YunAgentCollect yunAgentCollect = yunAgentCollectService.getOne(yunAgentCollectQueryWrapper);
        if(yunAgentCollect==null){
            //返回没有支付订单
            log.info(">>minjie "+ResultEnum.YUN_REFUND_ORDER_NOTEXIT.getMessage());
            return ResultBodyUtils.build(ResultEnum.YUN_REFUND_ORDER_NOTEXIT.getCode(),ResultEnum.YUN_REFUND_ORDER_NOTEXIT.getMessage());
        }
        if(!yunAgentCollect.getAmount().equals(amount)){
            //金额和订单金额匹配
            log.info(">>minjie "+ResultEnum.YUN_REFUND_AMOUNT_ERROR.getMessage());
            return ResultBodyUtils.build(ResultEnum.YUN_REFUND_AMOUNT_ERROR.getCode(),ResultEnum.YUN_REFUND_AMOUNT_ERROR.getMessage());
        }
        Long longAMount = new Long ((long)amount);
        HashMap<String, Object> refundMap = yunRefundService.refund(yunAgentCollect.getPayerId(),orderId,yunAgentCollect.getBizOrderNo(),longAMount);
        if(refundMap.get("status").equals("OK")){
            YunRefund yunRefund = (YunRefund)  refundMap.get("yunRefund");
            refundVO.setAmount(amount);
            refundVO.setNumber(yunRefund.getBizOrderNo());
            refundVO.setOrderId(orderId);
            refundVO.setCt(yunRefund.getCt());
        }else{
            log.info(">>minjie "+refundMap.get("message").toString());
            return ResultBodyUtils.build(ResultEnum.YUN_REFUND_NOT_FOUND.getCode(),refundMap.get("message").toString());
        }
        return ResultBodyUtils.ok(refundVO);
    }

    @Override
    public ResultBody<RefundVO> selectRefund(Long orderId) {
        RefundVO refundVO = new RefundVO();
        QueryWrapper<YunRefund> yunRefundQueryWrapper = new QueryWrapper<YunRefund>();
        yunRefundQueryWrapper.eq("order_id",orderId);
        YunRefund yunRefund = yunRefundService.getOne(yunRefundQueryWrapper);
        if(yunRefund==null){
            return ResultBodyUtils.build(ResultEnum.YUN_REFUND_NOT_FOUND.getCode(),ResultEnum.YUN_REFUND_NOT_FOUND.getMessage());
        }
        refundVO.setOrderId(orderId);
        refundVO.setNumber(yunRefund.getBizOrderNo());
        refundVO.setAmount(yunRefund.getAmount());
        refundVO.setCt(yunRefund.getCt());
        return ResultBodyUtils.ok(refundVO);
    }
}
